/************************************************************************
* Controller class, handle user interaction, update model
* Author : Shaolan
* Version : 1.0
* Website : http://shaolan.net
*/
#include "controller.hpp"

// ***********************************************************************************************************************************
// Public Constructor
Controller::Controller(){
	setModel(NULL);
	input = geCreateKeys();
}

Controller::Controller(Model * m){
	setModel(m);
	input = geCreateKeys();
}

int Controller::Keys[] = { GEK_UP , GEK_DOWN , GEK_RIGHT , GEK_LEFT , GEK_CROSS , GEK_CIRCLE , GEK_SQUARE , GEK_TRIANGLE };
Controller::Callback Controller::action[] = { 	&Controller::upCallback , &Controller::downCallback , 
									&Controller::rightCallback , &Controller::leftCallback , 
									&Controller::crossCallback , &Controller::circleCallback , 
									&Controller::squareCallback , &Controller::triangleCallback };

// ***********************************************************************************************************************************
// Public Destructor
Controller::~Controller(){
}

// ***********************************************************************************************************************************
// Getters
Model * Controller::getModel() const { return model; }

// ***********************************************************************************************************************************
// Setters
void Controller::setModel(Model * m) { model = m; }

// ***********************************************************************************************************************************
// Controller callback method

Signal Controller::crossCallback(){
	return SIGDONE;
}

Signal Controller::circleCallback(){
	return SIGDONE;
}

Signal Controller::squareCallback(){
	return SIGDONE;
}

Signal Controller::triangleCallback(){
	return SIGDONE;
}

Signal Controller::upCallback(){
	return SIGDONE;
}

Signal Controller::downCallback(){
	return SIGDONE;
}

Signal Controller::leftCallback(){
	return SIGDONE;
}

Signal Controller::rightCallback(){
	return SIGDONE;
}


// ***********************************************************************************************************************************
// Update method : read user input, and check game state
bool Controller::update(){
	// Update Model 
	// Read key 
	geReadKeys(input);
	for (int i = 0 ; i < N_KEYS; i++){
		if (input->pressed[Keys[i]]){
			Signal s = (this->*action[i])();
			if (s == SIGWIN || s == SIGLOST) return false;
	}}
	return true;
}